In this article, we will discuss a few best practices that we need to follow while developing the Persistence/DAO layer.
TheData Access Object (DAO) pattern is a structural pattern that allows us to isolate the application/business layer from the persistence layer (usually a relational database, but it could be any other persistence mechanism) using an abstract API.
Check out DAO pattern at Data Access Object (DAO) pattern
These are the commonly used database table operations for every table so we can make these operations generic in the DAO layer.
In the Java community, we have different options to develop Persistence/DAO layer
The above diagram shows a 3 layer architecture in any typical Spring MVC web applications.
Presentation layer:This is the user interface of the application that presents the application’s features and data to the user.
Business logic (or Application) layer:This layer contains the business logic that drives the application’s core functionalities. Like making decisions, calculations, evaluations, and processing the data passing between the other two layers.
Data access layer (or Data) layer:This layer is responsible for interacting with databases to save and restore application data.
1. Separate Layer for Persistence logic or database-related code We should maintain persistent logic as a separate layer ( DAO Layer or Persistence Layer)
2. In persistence logic, we should follow the table per DAO class. For every table, we will create a dedicated DAO class and this DAO class is responsible to perform operations with That table ( which improves readability)
For example:
USER -> UserDao.java
CUSTOMER -> CustomerDao.java
ROLES -> RolesDao.java
ACCOUNT_DETAILS -> AccountDetailsDao.java
USER -> UserDao.java
UserDaoImpl.java
CUSTOMER -> CustomerDao.java
CustomerDaoImpl.java
ROLES -> RolesDao.java
RolesDaoImpl.java
ACCOUNT_DETAILS -> AccountDetailsDao.java
AccountDetailsDaoImpl.java
3.AccountDetailsDaoImpl.java
4. Auditing columns:
-> CREATED_DATE
-> CREATED_BY
-> UPDATED_DATE
-> UPDATE_BY